home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 21 / Cream of the Crop 21 (Terry Blount) (October 1996).iso / educate / wordy442.zip / VOWELS.C < prev    next >
C/C++ Source or Header  |  1996-06-20  |  6KB  |  238 lines

  1. /**************************************************************************/
  2. /*                             Vowel Number Utility                       */
  3. /*                                                                        */
  4. /*                                     M\Cooper                           */
  5. /*                                    PO Box 237                          */
  6. /*                            St. David, AZ 85630-0237                    */
  7. /*                        -------------------------------                 */
  8. /*                        Email:  thegrendel@theriver.com                 */
  9. /*                                                                        */
  10. /*                  $2.00 to register the entire WORDY package            */
  11. /*                                                                        */
  12. /**************************************************************************/
  13.  
  14.  
  15. #include <conio.h>
  16. #include "srch.h"
  17.  
  18.  
  19. #define FILE_OPENING_ERROR 3
  20. #define FILENAME_MAXLEN 8
  21. #define CR "\n"
  22. #define FILE_SUFFIX ".5v8"
  23. #define MAXLEN 30
  24. #define FNLEN 40
  25. #define LINE_LEN 80
  26. #define NOARGS 1
  27. #define FILEARGS 2
  28. #define INCREMENT 1
  29. #define SPACE ' '
  30. #define XOUT '@'
  31. #define WILDCARD '?'
  32. #define FILLCHAR '_'
  33. #define BINGO 7
  34. #define BUFFERSIZE 8192
  35.  
  36. char ad[] =
  37. "VOWEL utility by M\\Cooper, PO Box 237, St. David, AZ 85630-0237";
  38.  
  39.  
  40. typedef enum { FALSE, TRUE } Boolean;
  41.  
  42.  
  43. void getword( int no_of_vowels, char *sourcefile );
  44. void center( char *strng );
  45. Boolean wordtest( int vn, char *wrd );
  46.  
  47.  
  48. void main( int argc, char **argv )
  49. {
  50.  
  51.    char vowelnum [ MAXLEN ],
  52.         wordfile [ FNLEN ];
  53.    int vn,
  54.        wl;
  55.  
  56.      if( argc < FILEARGS )
  57.         {
  58.         clrscr();
  59.         puts("Enter number of vowels ");
  60.         gets( vowelnum );
  61.      vn = atoi( vowelnum );
  62.         }
  63.    else
  64.      vn = atoi( *( argv + 1 ) );
  65.  
  66.       if( argc > FILEARGS )
  67.           strcpy( wordfile, *( argv + 2 ) );
  68.       else
  69.           strcpy( wordfile, "word.lst" );
  70.          
  71.  
  72.      getword( vn, wordfile );
  73. }
  74.  
  75.  
  76.  
  77. /**********************************WORDTEST********************************/
  78. /*       Function tests if word contains specified number of vowels       */
  79. /*         Returns: error_flag == TRUE (1) if yes, FALSE (0) if no        */
  80. /**************************************************************************/
  81.  
  82. Boolean wordtest( int vowelnum, char *word )
  83. {
  84.    char v_lset [] = "aeiou";
  85.    int vowels_counted = 0;
  86.  
  87.       while( *word )
  88.          {
  89.          if( strchr( v_lset, *word++ ) != NULL )
  90.             vowels_counted++;
  91.          }
  92.  
  93.       if( vowels_counted == vowelnum )
  94.           return( TRUE );
  95.       else
  96.           return( FALSE );
  97. }
  98.  
  99. /*************************************************************/
  100. void getword( int vowelnum, char *sourcefile )
  101. {
  102.  
  103.     char file_name[ MAXLEN ],
  104.          word [ MAXLEN ],
  105.          tempstr [ MAXLEN + 1 ],
  106.          tstr [ MAXLEN ],
  107.          targetfile [ MAXLEN ],
  108.          bar [ LINE_LEN + 1 ],
  109.          double_bar [ LINE_LEN + 1 ],
  110.         *ppos;
  111.  
  112.    FILE *fptr,
  113.         *tfile;
  114.    int fnamelen,
  115.        i;
  116.    long wcount = 0L;
  117.  
  118.  
  119.        strcpy( file_name, "word.lst" );
  120.  
  121.        memset( bar, '-', LINE_LEN );
  122.        *( bar + LINE_LEN ) = NULL;
  123.        memset( double_bar, '=', LINE_LEN );
  124.        *( double_bar + LINE_LEN ) = NULL;
  125.  
  126.        /*************opening credits*************/
  127.        clrscr();
  128.        printf( double_bar );
  129.        strcpy( tempstr, ad );
  130.        center ( tempstr );
  131.        printf( tempstr );
  132.     printf( CR );   /*****44444*****/
  133. //  printf( " " );  //Pad out line.
  134.        printf( double_bar );
  135.        printf( CR );
  136.        /****************************************/
  137.  
  138.  
  139.  
  140.        /*   Create name of file to store derived words in   */
  141.        /*********************************************************/
  142.        sprintf( tstr, "%d", vowelnum );   /***222***/
  143.        strcpy( targetfile, "vowels." );
  144.        strcat( targetfile, tstr );
  145.  
  146.  
  147.  
  148.        /*********************************************************/
  149.  
  150.  
  151.        if( !( fptr = fopen( sourcefile, "rt" ) ) ) /***ZZZ***/
  152.          {
  153.          printf( "\7\7\7Cannot open Wordfile: %s!", file_name );
  154.          exit( FILE_OPENING_ERROR );
  155.          }
  156.       if( setvbuf( fptr, NULL, _IOFBF, 2 * BUFFERSIZE ) )
  157.          exit( FILE_OPENING_ERROR );
  158.  
  159.        if( !( tfile = fopen( targetfile, "wt" ) ) )
  160.          {
  161.          printf( "\7\7\7Cannot open file to save words in!" );
  162.          exit ( FILE_OPENING_ERROR + 2 );
  163.          }
  164.       if( setvbuf( tfile, NULL, _IOFBF, BUFFERSIZE ) )
  165.          exit( FILE_OPENING_ERROR );
  166.  
  167.        /**************'Wait' Message***************/
  168.        printf( CR CR );
  169.        printf( "WORKING...\n\n" );
  170.        printf( "This will take a few seconds...\n" );
  171.        printf( "Please be patient.\n\n" );
  172.        printf( "Now searching 100,000+ word file and writing file.\n\n" );
  173.        /********************************************/
  174.  
  175.  
  176.  
  177.  
  178.       fprintf( tfile, double_bar );
  179.       fprintf( tfile, CR );
  180.       sprintf( tempstr, "Words with %d vowel(s):", vowelnum );
  181.       center( tempstr );
  182.       fprintf( tfile, tempstr );
  183.       fprintf( tfile, CR );     /***03333***/
  184.  
  185.       fprintf( tfile, double_bar );
  186.       fprintf( tfile, CR );
  187.       fprintf( tfile, CR );
  188.  
  189.  
  190.          /*********************Main Loop*************/     
  191.           while( fgets( word, MAXLEN, fptr ) != NULL )
  192.  
  193.             if( wordtest( vowelnum, word )  ) 
  194.                {
  195.                fprintf( tfile, "%s", word );
  196.                wcount++;
  197.                }
  198.           /*******************************************/
  199.  
  200.  
  201.           fprintf( tfile, bar );
  202.       fprintf( tfile, CR );
  203.           sprintf( tempstr, "%ld words in the list have %d vowel(s).",
  204.                  wcount, vowelnum );
  205.           center( tempstr );              
  206.           fprintf( tfile, tempstr );
  207.           fprintf( tfile, "\n\n" );
  208.  
  209.           center( ad );
  210.           fprintf( tfile, ad );
  211.  
  212.           fcloseall();
  213.  
  214.           sprintf( tempstr,
  215.                  "The file %s has %ld words with %d vowel(s)\7.",
  216.                  targetfile, wcount, vowelnum );
  217.           center( tempstr );
  218.           printf( CR CR );
  219.           printf( tempstr );
  220.  
  221. }
  222.  
  223.  
  224.  
  225. void center( char *str )
  226. {
  227.    int padding;
  228.    char st [ LINE_LEN + INCREMENT ];
  229.  
  230.      padding = LINE_LEN / 2 - strlen( str ) / 2;
  231.      memset( st, SPACE, padding );
  232.      *( st + padding ) = NULL;  //Terminate string
  233.      strcat( st, str );
  234.      strcpy( str, st );
  235.  
  236.      return;
  237. }
  238.